#include "../CGame.h"
#include "CMainMenuState.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "../../MasLiteServer/ObjectManager.h"
CMainMenuState::CMainMenuState()
{
	_pTM = CSGD_TextureManager::GetInstance();
	_pDI = CSGD_DirectInput::GetInstance();
}

CMainMenuState::~CMainMenuState()
{
	_pTM = NULL;
}

CMainMenuState* CMainMenuState::GetInstance()
{
	static CMainMenuState cedric;
	return &cedric;
}

void CMainMenuState::Enter(void)
{
	_nSelectedItem = 0;
	_nCurrentMenuState = 0;
	
	_nBackgroundID = _pTM->LoadTexture("resource/menu.png");
	_nCursorID = _pTM->LoadTexture("resource/cursor.png");
	int font = _pTM->LoadTexture("resource/metal.png");
	_bitmapFont.Initialize( font, ' ', 64, 64, 10 );
	_nRobotID =  _pTM->LoadTexture("resource/robot.png");
	//memset(_buffer,0,256);
	_nCurrentIndex = 0;
	client = &CGame::GetInstance()->client;
}
bool CMainMenuState::Input(void)
{
	switch(_nCurrentMenuState)
	{
	case 0:
		{
			if(_pDI->KeyPressed(DIK_ESCAPE))
				return false;
			if(_pDI->KeyPressed(DIK_RETURN))
			{
				_nCurrentMenuState = 1+_nSelectedItem;
				_nCurrentIndex = 0;
			}
			if(_pDI->KeyPressed(DIK_UP))
			{
				_nSelectedItem--;
				if(_nSelectedItem < 0)
					_nSelectedItem = 2;
			}
			if(_pDI->KeyPressed(DIK_DOWN))
			{
				_nSelectedItem++;
				if(_nSelectedItem > 2)
					_nSelectedItem = 0;
			}
			if(_pDI->KeyDown(DIK_RIGHT))
			{
				client->Move(1);
			}
			else if(_pDI->KeyDown(DIK_LEFT))
			{
				client->Move(-1);
			}
		}
		break;
	case 1:
		{
			if(_pDI->KeyPressed(DIK_ESCAPE))
				_nCurrentMenuState = 0;
		}
		break;
	case 2:
			if(_pDI->KeyPressed(DIK_ESCAPE))
				_nCurrentMenuState = 0;
		break;
	default:
		return false;
	}
	return true;
}
void CMainMenuState::Update(float fDT)
{
	ObjectManager::getInstance()->Update(fDT);
}
void CMainMenuState::Render(void)
{
	_pTM->Draw(_nBackgroundID, 0, 0);
	_bitmapFont.Print("Move and Shoot Lite",0,0);
	switch(_nCurrentMenuState)
	{
	case 0:
		{
			_pTM->Draw(_nCursorID, 0, 80+20*_nSelectedItem);
			_bitmapFont.Print("Play",40,80,0.5f);
			_bitmapFont.Print("Options",40,100,0.5f);
			_bitmapFont.Print("Exit",40,120,0.5f);
		}
		break;
	case 1:
		{
			_bitmapFont.Print("Enter IP",40,80,0.5f);
			_bitmapFont.Print(_buffer,40,80,0.5f);
		}
		break;
	}
	sORobot* rob;
	for(unsigned int i =0;i<ObjectManager::getInstance()->m_liRobots.size();i++)
	{
		rob = ObjectManager::getInstance()->GetRobot(i);

		if( rob )
		_pTM->Draw(_nRobotID, rob->m_fPos.vX, rob->m_fPos.vY,1.0f,1.0f,NULL,0,0,0,rob->m_fColor.cR,rob->m_fColor.cG,rob->m_fColor.cB);
	}
}
void CMainMenuState::Exit(void)
{
	_pTM->UnloadTexture(_nBackgroundID);
}